CREATE
OR REPLACE PROCEDURE zcrk_auto_to_fcckhc () AS
v_date_str   VARCHAR2(8);  -- 日期部分 YYYYMMDD
    v_seq        NUMBER(4);    -- 流水号
    p_bill_no VARCHAR2(50);
    insert_phid NUMBER;
 BEGIN
    -- 获取当天日期字符串
    v_date_str := TO_CHAR(SYSDATE, 'YYYYMMDD');

    -- 查询当天的最大流水号
    SELECT NVL(MAX(TO_NUMBER(SUBSTR(bill_no, 9, 4))), 0) + 1
      INTO v_seq
      FROM p_form_fcckhcd_m
     WHERE SUBSTR(bill_no, 1, 8) = v_date_str;

    -- 拼接编码 = 日期 + 流水号
    p_bill_no := v_date_str || LPAD(v_seq, 4, '0');

-- 直接 FOR 循环查询结果
    FOR rec IN (
        SELECT
            *
        FROM
            kc_billhead t
        WHERE
            t.user_cllx = '02' and phid_transno = '6' and NOT EXISTS (
            SELECT 1
            FROM kc_billhead t2
            WHERE t2.bill_no = t.bill_no and phid_transno = '6'
            GROUP BY t2.bill_no
            HAVING COUNT(*) > 1
        )
    ) LOOP
    -- 判断条件
    IF rec.tempflg = 2 THEN
    insert_phid :=seq_auto_phid_fcckhc.nextval;
    -- TODO: 在这里写逻辑
        INSERT INTO
            p_form_fcckhcd_m (
                phid,          -- 01
                code,          -- 02
                creator,       -- 03
                editor,        -- 04
                ng_insert_dt,  -- 05
                ng_update_dt,  -- 06
                ng_record_ver, -- 07
                cur_orgid,     -- 08
                ng_phid_org,   -- 09
                ng_phid_cu,    -- 10
                ng_phid_bp,    -- 11
                ng_phid_original, -- 12
                ng_orgid_original, -- 13
                ng_phid_ui_scheme, -- 14
                ng_sv_search_key,  -- 15
                ng_sd_search_key,  -- 16
                ng_share_sign,     -- 17
                phid_schemeid,     -- 18
                imp_info,          -- 19
                ng_write_source,   -- 20
                ng_data_status,    -- 21
                arc_flag,          -- 22
                asr_flag,          -- 23
                printtime,         -- 24
                printcount,        -- 25
                phid_app,          -- 26
                app_dt,            -- 27
                app_status,        -- 28
                wf_flag,           -- 29
                phid_fill_psn,     -- 30
                phid_org,          -- 31
                phid_pc,           -- 32
                bill_dt,           -- 33
                bill_name,         -- 34
                bill_no,           -- 35
                u_cklx,            -- 36
                u_fbht,            -- 37
                u_fbd,             -- 38
                u_ckrq,            -- 39
                u_lldw,            -- 40
                u_bhsjexx,         -- 41
                u_bhsjedx,         -- 42
                u_ck               -- 43
            )
        VALUES
            (
                insert_phid,        -- 1
                null,               -- 2
                rec.creator,        -- 3
                rec.creator,        -- 4
                SYSDATE,            -- 5
                SYSDATE,            -- 6
                1,                  -- 7
                1,                  -- 8
                1,                  -- 9
                1,                  -- 10
                0,                  -- 11
                0,                  -- 12
                0,                  -- 13
                0,                  -- 14
                '10',               -- 15
                '1',                -- 16
                5,                  -- 17
                null,               -- 18
                null,               -- 19
                0,                  -- 20
                0,                  -- 21
                0,                  -- 22
                0,                  -- 23
                null,               -- 24
                0,                  -- 25
                null,               -- 26
                null,               -- 27
                0,                  -- 28
                0,                  -- 29
                rec.creator,        -- 30
                rec.phid_org,                  -- 31
                rec.phid_pc,               -- 32
                SYSDATE,            -- 33
                null,               -- 34
                p_bill_no,          -- 35
                rec.user_cklx,               -- 36
                rec.user_fbht,               -- 37
                rec.user_fbd,               -- 38
                rec.user_ckrq,               -- 39
                rec.phid_classno,               -- 40
                0 - COALESCE(rec.user_bhsjexx,0), -- -5 不含税金额（元）小写
                DECODE(rec.user_bhsjey, NULL, '零', '负' || rec.user_bhsjey), -- -4 不含税金额（元）大写
                rec.phid_warehouse                -- 43
            );
                    -- 处理明细
            FOR rec_d IN (SELECT * FROM kc_billbody WHERE pphid = rec.phid) LOOP
                INSERT INTO
                    p_form_fcckhcd_d1 (
                    phid,               -- 1
                        code,               -- 2
                        rel_key1,           -- 3
                        asr_flag,           -- 4
                        imp_info,           -- 5
                        creator,            -- 6
                        editor,             -- 7
                        ng_insert_dt,       -- 8
                        ng_update_dt,       -- 9
                        ng_record_ver,      -- 10
                        cur_orgid,          -- 11
                        ng_phid_org,        -- 12
                        ng_phid_cu,         -- 13
                        ng_phid_bp,         -- 14
                        ng_phid_original,   -- 15
                        ng_orgid_original,  -- 16
                        ng_phid_ui_scheme,  -- 17
                        ng_sv_search_key,   -- 18
                        ng_sd_search_key,   -- 19
                        ng_share_sign,      -- 20
                        ng_write_source,    -- 21
                        ng_data_status,     -- 22
                        lineid,             -- 23
                        pphid,              -- 24
                        phid_pc,            -- 25
                        itemid,             -- 26
                        res_propertys,      -- 27
                        res_masterdata,     -- 28
                        phid_resbs,         -- 29
                        u_kcsl,             -- 30
                        u_cksl,             -- 31
                        u_hshj,             -- 32
                        u_hsdj,             -- 33
                        u_sl,               -- 34
                        u_se,               -- 35
                        u_bhsdj,            -- 36
                        u_bhshj             -- 37
                    )
                VALUES
                    (
                        seq_auto_phid_fcckhc_detail.nextval,       -- 1
                        null,           -- 2
                        null,           -- 3
                        0,              -- 4
                        null,           -- 5
                        rec.creator,    -- 6
                        rec.creator,    -- 7
                        SYSDATE,        -- 8
                        SYSDATE,        -- 9
                        1,              -- 10
                        1,              -- 11
                        1,              -- 12
                        1,              -- 13
                        0,              -- 14
                        0,              -- 15
                        0,              -- 16
                        0,              -- 17
                        '10',           -- 18
                        '1',            -- 19
                        5,              -- 20
                        0,              -- 21
                        0,              -- 22
                        null,           -- 23
                        insert_phid,    -- 24
                        null,           -- 25
                        rec_d.phid_itemid,           -- 26
                        null,           -- 27
                        null,           -- 28
                        null,           -- 29
                        rec_d.user_kcsl,           -- 30
                        0 - COALESCE(rec_d.qty,  0),          -- 31
                        0 - COALESCE(rec_d.tax_mony,  0),          -- 32
                        rec_d.taxprc,           -- 33
                        rec_d.taxrate,           -- 34
                        rec_d.tax,           -- 35
                        rec_d.prc,           -- 36
                        0 - COALESCE(rec_d.mony , 0)           -- 37

                    );
            END LOOP;
    
            v_seq := v_seq + 1;
            p_bill_no := v_date_str || LPAD(v_seq, 4, '0');
        END IF;



    END LOOP;

commit;

EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE ('插入失败，错误信息：' || SQLERRM);

RAISE;

ROLLBACK;

END zcrk_auto_to_fcckhc;